Telegram Group & Telegram Channel
Note #19 Reduce sturct size (maligned) 📄

Недавно много говорили, о том что одна и та же структура (type struct), с разными типами полей и с разным порядком, может занимать разное кол-во памяти в зависимости от того, как расположить в памяти компьютера с учетом всех отступов. Более детально прочитать можно в вики [1], почему именно так происходит.

Хотелось бы, чтобы компилятор самостоятельно делал такие оптимизации.

Возьмем структура A:
$ cat main.go
// A is my struct
type A struct {
a bool // 1 byte
b float64 // 8 bytes
c int32 // 4 bytes
}


Хорошо что есть отличный линтер[2], который напомнит нам, что можно что-то улучшить:

$ golangci-lint run --enable=maligned main.go
main.go:4:8: struct of size 24 bytes could be of size 16 bytes (maligned)
type A struct {
^


либо

go get github.com/mdempsky/maligned
$ maligned .
/Users/andrii/work/us/🇺🇦 Go на двоих/com.golang_for_two/main.go:4:8: struct of size 24 could be 16


Links:
- https://en.wikipedia.org/wiki/Data_structure_alignment
- https://github.com/mdempsky/maligned



tg-me.com/golang_for_two/43
Create:
Last Update:

Note #19 Reduce sturct size (maligned) 📄

Недавно много говорили, о том что одна и та же структура (type struct), с разными типами полей и с разным порядком, может занимать разное кол-во памяти в зависимости от того, как расположить в памяти компьютера с учетом всех отступов. Более детально прочитать можно в вики [1], почему именно так происходит.

Хотелось бы, чтобы компилятор самостоятельно делал такие оптимизации.

Возьмем структура A:

$ cat main.go
// A is my struct
type A struct {
a bool // 1 byte
b float64 // 8 bytes
c int32 // 4 bytes
}


Хорошо что есть отличный линтер[2], который напомнит нам, что можно что-то улучшить:

$ golangci-lint run --enable=maligned main.go
main.go:4:8: struct of size 24 bytes could be of size 16 bytes (maligned)
type A struct {
^


либо

go get github.com/mdempsky/maligned
$ maligned .
/Users/andrii/work/us/🇺🇦 Go на двоих/com.golang_for_two/main.go:4:8: struct of size 24 could be 16


Links:
- https://en.wikipedia.org/wiki/Data_structure_alignment
- https://github.com/mdempsky/maligned

BY 🇺🇦 Go for two :)


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_for_two/43

View MORE
Open in Telegram


🇺🇦 Go на двоих Telegram | DID YOU KNOW?

Date: |

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

🇺🇦 Go на двоих from us


Telegram 🇺🇦 Go for two :)
FROM USA